/**
 * 文章修改实时检测&缓存
 */

$(function(){
	unsetunload();
	var loop_time = 2000;
	
	var xheditor_onload = function(){
		if (typeof editor == 'object'){
			ajax('/public/a_content_cache','',function(msg){
				if (msg.replace(/^(<br[^>]*>)+/g,'').length > 0)
					if (confirm('发现上次未保存的文章内容，是否继续编辑?'))
						editor.setSource(msg);
			});
			window.a_content = editor.getSource();
			setTimeout(xheditor_change,loop_time);
			return true;
		}
		setTimeout(xheditor_onload,1500);
	};
	setTimeout(xheditor_onload,1500);
	
	var xheditor_change = function(){
		a_content_cache = editor.getSource();
		if (window.a_content != a_content_cache){
			ajax('/public/a_content_cache','a_content='+encodeURIComponent(a_content_cache),function(msg){
				window.a_content = a_content_cache;
				beforeunload('文章内容已有修改，退出将丢失修改，确定?');
				window.onunload = function(){
					ajax('/public/a_content_cache','a_content=');
				};
				setTimeout(xheditor_change,loop_time);
			});
		}else
			setTimeout(xheditor_change,loop_time);
	}
	
	$('#main form').submit(function(){
		unsetunload();
		window.onunload = function(){
			ajax('/public/a_content_cache','a_content=');
		};
	});
})